package com.ssh.admin.action;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionSupport;
import com.ssh.common.util.JsonResult;
import com.ssh.common.util.Page;
import com.ssh.entity.ChangGui;
import com.ssh.entity.Employee;
import com.ssh.entity.SuperMarket;
import com.ssh.entity.Task;
import com.ssh.manager.ChangGuiManager;
import com.ssh.manager.impl.ChangGuiManagerImpl;

@Controller
@ParentPackage("admin")
@Scope("prototype")
@Results(value={
@Result(name="success", location="/admin/changGui/changGui.jsp"),
@Result(name="findList", type="json", params={"root","page","excludeproperties","page\\.curPageNum,page\\.totalPageNum,page\\.totalCount," +
"page\\.numberPerPage,page\\.pageData\\[\\d+\\]\\.id,page\\.pageData\\[\\d+\\]\\.fenxiao,page\\.pageData\\[\\d+\\]\\.stock,page\\.pageData\\[\\d+\\]\\.price," +
"page\\.pageData\\[\\d+\\]\\.chenlie,page\\.pageData\\[\\d+\\]\\.createDate,page\\.pageData\\[\\d+\\]\\.taskProductEmployee\\.employee\\.userName," +
"page\\.pageData\\[\\d+\\]\\.taskProductEmployee\\.taskBrand\\.task\\.content,page\\.pageData\\[\\d+\\]\\.taskProductEmployee\\.taskBrand\\.brand\\.name," +
"page\\.pageData\\[\\d+\\]\\.taskProductEmployee\\.superMarketProduct\\.superMarket\\.name,page\\.pageData\\[\\d+\\]\\.taskProductEmployee\\.superMarketProduct\\.product\\.name"}),
@Result(name="delete", type="json", params={"root","page","excludeproperties","page\\.curPageNum"})
})
public class ChangGuiAction extends ActionSupport{
	@Resource(name="changGuiManagerImpl")
	private ChangGuiManager changGuiManager;
	
	private Page page = new Page();
	private JsonResult jr=new JsonResult();
	private String isDesc;
	private Task task;
	private Employee employee;
	private SuperMarket superMarket;
	private ChangGui changGui;
	private String ids;
	public ChangGui getChangGui() {
		return changGui;
	}
	public void setChangGui(ChangGui changGui) {
		this.changGui = changGui;
	}
	public String getIds() {
		return ids;
	}
	public void setIds(String ids) {
		this.ids = ids;
	}
	public Page getPage() {
		return page;
	}
	public void setPage(Page page) {
		this.page = page;
	}
	public JsonResult getJr() {
		return jr;
	}
	public void setJr(JsonResult jr) {
		this.jr = jr;
	}
	public String getIsDesc() {
		return isDesc;
	}
	public void setIsDesc(String isDesc) {
		this.isDesc = isDesc;
	}
	public Task getTask() {
		return task;
	}
	public void setTask(Task task) {
		this.task = task;
	}
	public Employee getEmployee() {
		return employee;
	}
	public void setEmployee(Employee employee) {
		this.employee = employee;
	}
	public SuperMarket getSuperMarket() {
		return superMarket;
	}
	public void setSuperMarket(SuperMarket superMarket) {
		this.superMarket = superMarket;
	}
	
	public String goJsp(){
		return "success";
	}
	
	public String add(){
		return "success";
	}
	
	public String update(){
		return "";
	}
	
	public String delete(){
		try {
			if(ids!=null && !ids.equals("")){
				String[] d = ids.split(",");
				for (int i = 0; i < d.length; i++) {
					changGuiManager.delete(new Long(d[i]));
				}
			}else{
				changGuiManager.delete(changGui.getId());
			}
			int _p = (int) page.getCurPageNum();
			Map conditionMap = new HashMap();
			conditionMap.put("employee", employee.getId());
			conditionMap.put("task", task.getContent());
			conditionMap.put("market", superMarket.getId());
			changGuiManager.findList(conditionMap, page, isDesc);
			if(_p<=page.getTotalPageNum()){
				page.setCurPageNum(_p);
			}else{
				page.setCurPageNum(_p-1);
			}
			jr.setCode("y");
		} catch (Exception e) {
			e.printStackTrace();
			jr.setCode("n");
		}
		
		return "delete";
	}
	
	public String findList(){
		Map conditionMap = new HashMap();
		conditionMap.put("employee", employee.getId());
		conditionMap.put("task", task.getContent());
		conditionMap.put("market", superMarket.getId());
		changGuiManager.findList(conditionMap, page, isDesc);
		return "findList";
	}
}
